package com.wsc.wisdomchinaqinghe.activitity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.AbPermissionUtils;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.constant.Constant;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.SoftReference;

public class SplashActivity extends BaseActivity {

    private Context context;
    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;
    private static final long SPLASH_DELAY_MILLIS = 1000;// 延时3秒

        // handler 消息处理
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    // 进入HomeActivity界面
                    goHome();
                    break;

                case GO_GUIDE:
                    // 进入GuideActivity界面
                    goGuide();
                    break;

            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        context = SplashActivity.this;

        try{
            AbPermissionUtils.requestPermissionsResult(this, 1
                    , new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                    }
                    , new AbPermissionUtils.OnPermissionListener() {
                        @Override
                        public void onPermissionGranted() {
                            final Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
                            File cacheDir;
                            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                                cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), Constant.wsc_photo);
                            else
                                cacheDir = SplashActivity.this.getCacheDir();
                            if (!cacheDir.exists())
                                cacheDir.mkdirs();
                            final File file = new File(cacheDir, "taohua.png");
                            if (file.exists()) {
                                file.delete();
                            }

                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub
                                    FileOutputStream outStream = null;

                                    try {
                                        outStream = new FileOutputStream(file);
                                        if (bm != null) {
                                            SoftReference<Bitmap> softBitmap = new SoftReference<Bitmap>(bm);
                                            if (softBitmap != null && softBitmap.get() != null)
                                                softBitmap.get().compress(Bitmap.CompressFormat.PNG, 100, outStream);
                                        }
                                    } catch (FileNotFoundException e) {
                                        // TODO Auto-generated catch block
                                        // e.printStackTrace();
                                    }

                                    try {
                                        if (outStream != null) {
                                            outStream.flush();
                                            outStream.close();
                                        }
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }).start();
                        }

                        @Override
                        public void onPermissionDenied() {

                        }
                    });
        }catch (Exception e)
        {
        }

        initView();


    }

    // 初始化
    private void initView() {
        // 获取Sharepreference 第一次进入界面的值
        boolean isFirstIn = Tools.getSharedPreferencesboolen(Constant.isFirstIn, true, context);
        // 为第一次进入,就进入引导页面
        if (isFirstIn) {
            mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
//            goGuide();
        }
        // 非第一次进入，就进入主界面
        else {
            mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
//            goHome();
        }
    }

    /**
     * @Description 进入主界面
     * @author jaily.zhang
     */
    private void goHome() {
        Intent intent = new Intent(context, HomeActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * @Description 进入引导界面
     * @author jaily.zhang
     */
    private void goGuide() {
        Intent intent = new Intent(context, GuideActivity.class);
        startActivity(intent);
        finish();
    }

}
